Imports System.Data.Common
Imports System.Net.Mail
Partial Class Presentation_CreateCompany
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim Sql As String
        Dim Records As DbDataReader

        Page.Title = "Company Entry"

        If Not Page.IsPostBack Then

            If PreviousPage IsNot Nothing Then

                If PreviousPage.IsCrossPagePostBack Then

                    PostBack.Value = PreviousPage.GetUniqueID
                    Sql = "SELECT ClientName, Address1, Address2, Address3, City, Pincode, Profile, Comments" & _
                        " FROM ClientMaster WHERE ClientID = " & PostBack.Value

                    Records = GenHelper.ExecuteReader(Sql, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())

                    'Fill the fields with the data to be updated

                    If Records.Read Then

                        txtClientName.Text = Records.Item("ClientName").ToString
                        txtAddress1.Text = Records.Item("Address1").ToString
                        txtAddress2.Text = Records.Item("Address2").ToString
                        txtAddress3.Text = Records.Item("Address3").ToString
                        txtCity.Text = Records.Item("City").ToString
                        txtPinCode.Text = Records.Item("PinCode").ToString
                        txtProfile.Text = Records.Item("Profile").ToString
                        txtComments.Text = Records.Item("Comments").ToString

                        txtContactPerson.Enabled = False
                        txtContactPerson.Text = "Disabled"
                        txtDesignation.Enabled = False
                        txtTelephone.Enabled = False
                        txtTelephone.Text = "Disabled"
                        txtemail.Enabled = False

                    End If


                End If

            Else
                PostBack.Value = False

            End If

        End If

    End Sub

    Protected Sub ValidateTelEmail(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvdTelEmail.ServerValidate

        If txtTelephone.Text = "" And txtemail.Text = "" And txtMobile.Text = "" Then
            args.IsValid = False
        Else
            args.IsValid = True
        End If

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        If PostBack.Value = False Then
            InsertClient()
        Else
            UpdateClient(PostBack.Value)
        End If


    End Sub

    Protected Sub InsertClient()

        Dim Sql As String
        Dim RightNow As Date
        Dim Params(0) As GenParameter
        Dim TDate As Date
        Dim LastInsertedID As Integer
        Dim NumClient As Integer
        Dim Reader As DbDataReader
        Dim hasrows As Boolean
        Dim count As Integer

        RightNow = Now.AddHours(11)
        RightNow = RightNow.AddMinutes(30)
        TDate = RightNow.Year & "/" & RightNow.Month & "/" & RightNow.Day

        'Hide the List of Employee names with the client
        Handling.Visible = False

        If Not Page.IsValid() Then
            lblSuccess.Visible = False
            Return
        End If

        'Check and see if it's a duplicate entry

        Sql = "SELECT COUNT(*) from ClientMaster WHERE ClientName = ?ClientName"

        Params(0) = New GenParameter("?ClientName", txtClientName.Text.Trim, Data.DbType.String)

        NumClient = GenHelper.ExecuteScalar(Sql, Params, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())

        'Create the error texts if there is a duplicate company

        If (NumClient <> 0) Then
            lblSuccess.Text = "This Client already exists in the Database. The following people are handling it"
            lblSuccess.Visible = True
            Sql = "SELECT DISTINCT ExecutiveMaster.ExecutiveName AS 'Name', ExecutiveMaster.Location AS 'Location'" & _
                    " FROM (CompanyAssignments INNER JOIN ExecutiveMaster ON CompanyAssignments.ExecutiveID = ExecutiveMaster.ExecutiveID) INNER JOIN ClientMaster ON CompanyAssignments.ClientID = ClientMaster.ClientID" & _
                    " WHERE ClientName = ?ClientName"

            Reader = GenHelper.ExecuteReader(Sql, Params, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())
            Handling.DataSource = Reader
            hasrows = Reader.HasRows
            count = Reader.RecordsAffected
            Handling.DataBind()
            Handling.Visible = True
            Reader.Close()
            Return
        End If

        'Make the Entry into ClientMaster

        ReDim Params(7)

        Sql = "INSERT INTO ClientMaster (ClientName,Address1, Address2, Address3, City" & _
        ", Pincode, Profile, Comments) VALUES (?ClientName,?Address1, ?Address2, ?Address3" & _
        ", ?City, ?Pincode, ?Profile, ?Comments);Select LAST_INSERT_ID() AS ID"

        Params(0) = New GenParameter("?ClientName", StrConv(txtClientName.Text.Trim(), VbStrConv.ProperCase), Data.DbType.String)
        Params(1) = New GenParameter("?Address1", txtAddress1.Text, Data.DbType.String)
        Params(2) = New GenParameter("?Address2", txtAddress2.Text, Data.DbType.String)
        Params(3) = New GenParameter("?Address3", txtAddress3.Text, Data.DbType.String)
        Params(4) = New GenParameter("?City", txtCity.Text, Data.DbType.String)
        Params(5) = New GenParameter("?Pincode", txtPinCode.Text, Data.DbType.String)
        Params(6) = New GenParameter("?Profile", txtProfile.Text, Data.DbType.String)
        Params(7) = New GenParameter("?Comments", txtComments.Text, Data.DbType.String)

        LastInsertedID = GenHelper.ExecuteScalar(Sql, Params, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())

        'Make the Entry into ContactMaster

        Sql = "INSERT INTO ContactMaster (ContactName, ContactEmail, Telephone, Mobile, ClientID" & _
        ", Designation, DecisionMaker) VALUES (?ContactName, ?ContactEmail, ?Telephone, ?Mobile, ?ClientID" & _
        ", ?Designation, ?DecisionMaker)"

        ReDim Params(6)

        Params(0) = New GenParameter("?ContactName", txtContactPerson.Text, Data.DbType.String)
        Params(1) = New GenParameter("?ContactEmail", txtemail.Text, Data.DbType.String)
        Params(2) = New GenParameter("?Telephone", txtTelephone.Text, Data.DbType.String)
        Params(3) = New GenParameter("?Mobile", txtMobile.Text, Data.DbType.String)
        Params(4) = New GenParameter("?ClientID", LastInsertedID, Data.DbType.UInt32)
        Params(5) = New GenParameter("?Designation", txtDesignation.Text, Data.DbType.String)
        Params(6) = New GenParameter("?DecisionMaker", 1, Data.DbType.Boolean)

        GenHelper.ExecuteNonQuery(Sql, Params, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())


        'Make the Entry into CompanyAssignments

        Sql = "INSERT INTO CompanyAssignments (ExecutiveID, ClientID, CADate) VALUES (?ExecutiveID, ?ClientID, ?CADate)"

        ReDim Params(2)

        Params(0) = New GenParameter("?ExecutiveID", Request.Cookies("Profile").Item("ExecutiveID"), Data.DbType.UInt32)
        Params(1) = New GenParameter("?ClientID", LastInsertedID, Data.DbType.UInt32)
        Params(2) = New GenParameter("?CADate", TDate, Data.DbType.Date)

        GenHelper.ExecuteNonQuery(Sql, Params, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())

        'Code to clear the controls
        txtClientName.Text = ""
        txtAddress1.Text = ""
        txtAddress2.Text = ""
        txtAddress3.Text = ""
        txtCity.Text = ""
        txtPinCode.Text = ""
        txtContactPerson.Text = ""
        txtDesignation.Text = ""
        txtTelephone.Text = ""
        txtemail.Text = ""
        txtProfile.Text = ""
        txtComments.Text = ""
        txtMobile.Text = ""
        lblSuccess.Text = "Successfully Saved"
        lblSuccess.Visible = True

    End Sub

    Protected Sub UpdateClient(ByVal ChangeID As Integer)

        Dim Sql As String
        Dim RightNow As Date
        Dim Params(0) As GenParameter
        Dim TDate As Date
        Dim NumClient As Integer
        Dim Reader As DbDataReader
        Dim hasrows As Boolean
        Dim count As Integer

        RightNow = Now.AddHours(11)
        RightNow = RightNow.AddMinutes(30)
        TDate = RightNow.Year & "/" & RightNow.Month & "/" & RightNow.Day

        'Hide the List of Employee names with the client
        Handling.Visible = False

        If Not Page.IsValid() Then
            lblSuccess.Visible = False
            Return
        End If

        'Check and see if it's a duplicate entry

        Sql = "SELECT COUNT(*) from ClientMaster WHERE ClientName = ?ClientName" & _
                " AND ClientID <> " & ChangeID

        Params(0) = New GenParameter("?ClientName", txtClientName.Text.Trim, Data.DbType.String)

        NumClient = GenHelper.ExecuteScalar(Sql, Params, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())

        'Create the error texts if there is a duplicate company

        If (NumClient <> 0) Then
            lblSuccess.Text = "This Client already exists in the Database. The following people are handling it"
            lblSuccess.Visible = True
            Sql = "SELECT DISTINCT ExecutiveMaster.ExecutiveName AS 'Name', ExecutiveMaster.Location AS 'Location'" & _
                    " FROM (CompanyAssignments INNER JOIN ExecutiveMaster ON CompanyAssignments.ExecutiveID = ExecutiveMaster.ExecutiveID) INNER JOIN ClientMaster ON CompanyAssignments.ClientID = ClientMaster.ClientID" & _
                    " WHERE ClientName = ?ClientName"

            Reader = GenHelper.ExecuteReader(Sql, Params, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())
            Handling.DataSource = Reader
            hasrows = Reader.HasRows
            count = Reader.RecordsAffected
            Handling.DataBind()
            Handling.Visible = True
            Reader.Close()
            Return
        End If

        'Make the Entry into ClientMaster

        ReDim Params(7)

        Sql = "UPDATE ClientMaster" & _
        " SET ClientName = ?ClientName, Address1 = ?Address1, Address2 = ?Address2, Address3 = ?Address3" & _
        ", City = ?City, PinCode = ?Pincode,Profile = ?Profile, Comments = ?Comments" & _
        " WHERE ClientID = " & ChangeID

        Params(0) = New GenParameter("?ClientName", StrConv(txtClientName.Text.Trim(), VbStrConv.ProperCase), Data.DbType.String)
        Params(1) = New GenParameter("?Address1", txtAddress1.Text, Data.DbType.String)
        Params(2) = New GenParameter("?Address2", txtAddress2.Text, Data.DbType.String)
        Params(3) = New GenParameter("?Address3", txtAddress3.Text, Data.DbType.String)
        Params(4) = New GenParameter("?City", txtCity.Text, Data.DbType.String)
        Params(5) = New GenParameter("?Pincode", txtPinCode.Text, Data.DbType.String)
        Params(6) = New GenParameter("?Profile", txtProfile.Text, Data.DbType.String)
        Params(7) = New GenParameter("?Comments", txtComments.Text, Data.DbType.String)

        GenHelper.ExecuteNonQuery(Sql, Params, ConfigurationManager.ConnectionStrings.Item("MySqlConnString").ToString())

        ' Send an Email
        Dim Message As String


        Dim Mail As New MailMessage()

        Mail.From = New MailAddress(User.Identity.Name)
        Mail.To.Add("bhagwad@zakgroup.com")
        Message = txtClientName.Text & " " & txtAddress1.Text & " " & txtAddress2.Text & " " & txtAddress3.Text & " " & txtCity.Text & " " & txtPinCode.Text & " " & txtProfile.Text & " " & txtComments.Text
        Mail.Subject = "Client's Details Changed"

        Mail.Body = Message

        Dim smtp As New SmtpClient("127.0.0.1")
        smtp.Send(Mail)

        

        'Code to clear the controls
        txtClientName.Text = ""
        txtAddress1.Text = ""
        txtAddress2.Text = ""
        txtAddress3.Text = ""
        txtCity.Text = ""
        txtPinCode.Text = ""
        txtContactPerson.Text = ""
        txtContactPerson.Enabled = True
        txtDesignation.Text = ""
        txtDesignation.Enabled = True
        txtTelephone.Text = ""
        txtTelephone.Enabled = True
        txtemail.Text = ""
        txtemail.Enabled = True
        txtProfile.Text = ""
        txtComments.Text = ""
        lblSuccess.Text = "Successfully Saved"
        lblSuccess.Visible = True

       

        'Put the page back into insert mode

        PostBack.Value = False

    End Sub

End Class
